package com.lanmao.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.lanmao.blog.utils.HttpUtil;
import com.lanmao.blog.entity.Music;
import com.lanmao.blog.service.MusicService;
import com.lanmao.blog.vo.data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MusicServiceImpl implements MusicService {
//    @Override
//    public SearchResult search(String key, int n) throws IOException {
//        SearchResult searchResult=new SearchResult();
//        Map<String,String> headers=new HashMap<>();
//        headers.put("Cookie","_ga=GA1.2.1453722430.1611324833; _gid=GA1.2.1326960712.1624172442; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1624172442; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1624174002; kw_token=DP2BRJBIPY; _gat=1");
//        headers.put("csrf","DP2BRJBIPY");
//        headers.put("Referer","https://www.kuwo.cn/search/list?key=%E9%82%93%E7%B4%AB%E6%A3%8B");
//        String url = "https://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key="+key+"&pn="+n+"&rn=30&httpsStatus=1&reqId=1df7e980-d199-11eb-9141-65650793c738";
//        Document document = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36").headers(headers).ignoreContentType(true).get();
//        String data = JSON.parseObject(document.body().text()).getString("data");
//        int total = JSON.parseObject(data).getInteger("total");
//        String list = JSON.parseObject(data).getString("list");
//        List<Music> music = JSON.parseArray(list, Music.class);
//        searchResult.setTotal(total);
//        searchResult.setData(music);
//        return searchResult;
//    }



    @Override
    public String mp4url(String rid) throws IOException {
        String url="https://www.kuwo.cn/api/v1/www/music/playUrl?mid="+rid+"&type=mv&httpsStatus=1&reqId=c8602b50-b0f3-11ec-afac-4d28f40e5e5b";
        //String url = "http://www.kuwo.cn/url?rid=" + rid + "&response=url&format=mp4%7Cmkv&type=convert_url&t=1624176391598&httpsStatus=1&reqId=6c3e1ce0-d19e-11eb-8f98-ff46322e29b0";
        String text = Jsoup.connect(url).ignoreContentType(true).get().text();
        data data = JSON.parseObject(text, data.class);
        return data.getData().get("url");//
    }


    @Override
    public List<Music> paihangb() {
        HttpUtil httpUtil = new HttpUtil();
        String body = httpUtil.GetHttp("http://www.kuwo.cn/api/www/bang/bang/musicList?bangId=16&pn=1&rn=100", 1);
        String data = JSON.parseObject(body).getString("data");
        String List = JSON.parseObject(data).getString("musicList");
        List<Music> musicList = JSON.parseArray(List, Music.class);
        return musicList;
    }

    @Override
    public String mp3url(String rid) throws Exception {
        String url="https://www.kuwo.cn/api/v1/www/music/playUrl?mid="+rid+"&type=music&httpsStatus=1&reqId=6e6aeeb0-b01b-11ec-bf20-13a3f734bd3b";
        String text = Jsoup.connect(url).ignoreContentType(true).get().text();
        data data = JSON.parseObject(text, data.class);
        return data.getData().get("url");//
    }
}
